package com.dz.lm.service.talkcomment.impl;

import com.dz.lm.common.dto.PageDto;
import com.dz.lm.common.dto.TalkCommentDto;
import com.dz.lm.common.exception.DaoException;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.dao.talkComment.read.TalkCommentDao;
import com.dz.lm.domain.talkcomment.TalkComment;
import com.dz.lm.service.talkcomment.TalkComenntService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2019/12/28 14:20;
 */
@Service
public class TalkComenntServiceImpl implements TalkComenntService {
    @Resource(name = "readTalkCommentDao")
    private TalkCommentDao readCommentDao;
    @Resource(name = "writeTalkCommentDao")
    private com.dz.lm.dao.talkComment.write.TalkCommentDao writeCommentDao;
    @Resource(name = "readAppLoginDao")
    private com.dz.lm.dao.appLogin.read.AppLoginDao rAppLoginDao;
    @Transactional
    @Override
    public int add(TalkComment comment) throws ServiceException {
        int i = 0;
        try {
            i = writeCommentDao.addTaklComment(comment);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return i ;
    }
    @Transactional
    @Override
    public int delte(Integer commentId) throws ServiceException {
        int i=0;
        try {
            i= writeCommentDao.deleteByPrimaryKey(commentId);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return i;
    }
    @Transactional
    @Override
    public int update(TalkComment comment) throws ServiceException {
        int i=0;
        try {
            i= writeCommentDao.updateTaklComment(comment);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public TalkCommentDto queryOne(Integer commentId) throws ServiceException {
        TalkCommentDto dto = new TalkCommentDto();
        TalkComment talkComment = null;
        try {
            talkComment = readCommentDao.selectByPrimaryKey(commentId);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        if(talkComment != null ){
            BeanUtils.copyProperties(talkComment,dto);
           setExt(dto);
        }
        return dto;
    }

    @Override
    public PageDto<TalkCommentDto> queryList(Map<String, Object> param, int page, int rows) throws ServiceException {
        PageDto<TalkCommentDto> pages = new PageDto<TalkCommentDto>();
        //使用插件  页码加1
        PageHelper.startPage(page+1,rows);
        List<TalkCommentDto> dtos =new ArrayList();
        param.put("type",1);
        param.put("order","DESC");
        List<TalkComment> talkComments = null;
        try {
            talkComments = readCommentDao.qryTalkCommentList(param);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        if(talkComments != null && talkComments.size()>0){
            PageInfo pageInfo = new PageInfo(talkComments);

            for (TalkComment comment : talkComments) {
                TalkCommentDto dto = new TalkCommentDto();
                BeanUtils.copyProperties(comment,dto);
                setUserInfo(dto);
                setExt(dto);
                dtos.add(dto);
            }
            pages.setList(dtos);
            pages.setPage(pageInfo.getPages());
            pages.setTotal(pageInfo.getTotal());
        }
        return pages;
    }

    /**
     * 添加补充信息
     * @param commentDto
     */
    private void setExt(TalkCommentDto commentDto) throws ServiceException{
        Map param = new HashMap();
        if(!StringUtil.containEmpty(commentDto.getContent())){
            commentDto.setContent(StringUtil.getURLDecoder(commentDto.getContent()));
        }
        List<TalkCommentDto> dtos = new ArrayList();
        param.put("parentId",commentDto.getCommentId());
        param.put("type",2);
        List<TalkComment>  list = null;
        try {
            list = readCommentDao.qryTalkCommentList(param);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        for (TalkComment comment : list) {
            TalkCommentDto dto = new TalkCommentDto();
            BeanUtils.copyProperties(comment,dto);
            setUserInfo(dto);
            dto.setContent(StringUtil.getURLDecoder(dto.getContent()));
            dtos.add(dto);
        }
        if(dtos!=null && dtos.size() > 0){
            commentDto.setSubComments(dtos);
        }
    }
    private void  setUserInfo(TalkCommentDto commentDto) throws ServiceException{
        Map param = new HashMap();
        param.put("userId",commentDto.getUserId());
        try {
            Map map = rAppLoginDao.qryUserInfoByUserId(param);
            if(map.get("loginName") != null){
                commentDto.setUserName(map.get("loginName").toString());
            }
            if(map.get("profile") != null ){
                commentDto.setProfile(map.get("profile").toString());
            }
        } catch (DaoException e) {
            new ServiceException(e.getCause());
        }
    }
}
